package cn.bgs.serviceedu.service.impl;

import cn.bgs.serviceedu.entity.Category;
import cn.bgs.serviceedu.dao.CategoryMapper;
import cn.bgs.serviceedu.service.CategoryService;
import cn.bgs.serviceedu.vo.RedisTop;
import cn.bgs.serviceutils.utils.CSException;
import cn.bgs.serviceutils.utils.RedisUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 种类表 服务实现类
 * </p>
 *
 * @author YiChen
 * @since 2021-10-20
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public List<Category> getListTop() {
        return categoryMapper.getListTop();
    }


    @Override
    public List<RedisTop> getRedisList() {
        //查询所有医生
        ArrayList<RedisTop> redisLists = null;
        try {
            Set<String> key = redisUtils.scan("category");
            Iterator<String> iterator = key.iterator();
            //创建集合存入 id 和 val
            redisLists = new ArrayList<>();
            while (iterator.hasNext()){
                String next = iterator.next();
                String[] split = next.split(":");
                String id =split[1];
                int val = (int) redisUtils.get(next);
                RedisTop redisList = new RedisTop(id,val);
                redisLists.add(redisList);
            }
        } catch (Exception e) {
            throw new CSException(20001,"医生遍历失败");
        }
        return redisLists;
    }
}
